/*
 * This project is mainly focused on developing more and more utilisties
 *  like mail utilities, dao utilities, ldap utilities, file utilities, 
 *  file importing utilities etc. for easy coding in java.
 *  
 *  Copyright (C) 2012  R.Satheesh Kumar alias GURU
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jsmart.util.io;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import org.jsmart.util.core.AbstractItemCallback;
import org.jsmart.util.core.AbstractItemFilter;
import org.jsmart.util.core.AbstractUtils;
import org.jsmart.util.core.AbstractQueryCaller;
import org.jsmart.util.core.UtilsException;

/**
 * This class is used for callback and performs the business logic also
 * the logical code is applied in the filtered items of the list. This class
 * extends the AbstractQueryCaller and performs the business logic with the
 * list data and filtering the data with the AbstractUtils.AbstractItemFilter
 * 
 * @author R.Satheesh Kumar alias GURU <pulsarcoder@gmail.com>
 * @since v1.0
 * @see AbstractQueryCaller
 * @see AbstractUtils
 */
public class QueryReaderCaller<T> extends AbstractQueryCaller<BufferedReader, List<T>, T, String> {
    
    public QueryReaderCaller(AbstractItemCallback<T, String> callback, 
            AbstractItemFilter<String> filter) {
        this(callback, filter, false);
    }
    
    public QueryReaderCaller(AbstractItemCallback<T, String> callback, 
            AbstractItemFilter<String> filter, Boolean singleData) {
        super(callback, filter, singleData);
    }
    /**
     * This method performs the iteration of all the list items and then injects 
     * the item into ListUtils.ListItemCallback and ListUtils.ListItemFilter
     * This is the implemented method because of implementing ListUtils.ListCallback
     * This method is injected with the InputStream object, hence with the help of the
     * InputStream this method gets the Reader object by,
     * <code>
     *  BufferedReader reader = new BufferedReader(new InputStreamReader(in));
     * </code>
     * Also this method acts in two different aspects one is returning the list of values that
     * matches the filter options and the second one is returns the list of single row that 
     * matches the filter options.
     * @param list
     * @return 
     */
    @Override
    public List<T> doInCallback(BufferedReader reader) throws UtilsException {
        List<T> list = new ArrayList<T>();
        String row;
        int rowNumber = 0;
        try {
            while ((row = reader.readLine()) != null) {
                if (filter.matches(row, rowNumber)) {
                    T t = callback.doInItem(row, rowNumber);
                    if (t != null) {
                        list.add(t);
                        if(singleData) return list;
                    }
                }
                rowNumber++;
            }
        } catch (Exception e) {
            throw new UtilsException("Error in performing logic code in line of reader object, due to " + e);
        }
        return list;
    }
}
